package org.jeecg.modules.basesource.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.basesource.entity.SbCwbbBw;
import org.jeecg.modules.basesource.mapper.SbCwbbBwMapper;
import org.jeecg.modules.basesource.service.ISbCwbbBwService;
import org.jeecg.modules.basesource.utils.cwbb.CwbbQykjzzWrapperUtil;
import org.jeecg.modules.basesource.utils.cwbb.CwbbXqyWrapperUtil;
import org.jeecg.modules.basesource.utils.cwbb.CwbbYbqyWrapperUtil;
import org.jeecg.modules.basesource.utils.cwbb.CwbbYbqyyzxWrapperUtil;
import org.jeecg.modules.dzswj.vo.response.cwbb.*;
import org.jeecg.modules.sssj.entity.SssjCwbbHnzLrb;
import org.jeecg.modules.sssj.entity.SssjCwbbHnzXjllb;
import org.jeecg.modules.sssj.entity.SssjCwbbHnzZcfzb;
import org.jeecg.modules.sssj.service.ISssjCwbbHnzLrbService;
import org.jeecg.modules.sssj.service.ISssjCwbbHnzXjllbService;
import org.jeecg.modules.sssj.service.ISssjCwbbHnzZcfzbService;
import org.jeecg.modules.sssj.vo.CwbbSssjVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description: 财务报表报文
 * @Author: jeecg-boot
 * @Date:   2022-03-03
 * @Version: V1.0
 */
@Service
public class SbCwbbBwServiceImpl extends MppServiceImpl<SbCwbbBwMapper, SbCwbbBw> implements ISbCwbbBwService {

    @Autowired
    private ISssjCwbbHnzLrbService sssjCwbbHnzLrbService;
    @Autowired
    private ISssjCwbbHnzXjllbService sssjCwbbHnzXjllbService;
    @Autowired
    private ISssjCwbbHnzZcfzbService sssjCwbbHnzZcfzbService;

    @Override
    public SbCwbbBw getByCompositePk(String tynsrsbh, String ssqq, String ssqz) {
        LambdaQueryWrapper<SbCwbbBw> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SbCwbbBw::getTynsrsbh, tynsrsbh).eq(SbCwbbBw::getSkssqq, ssqq).eq(SbCwbbBw::getSkssqz, ssqz);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<SbCwbbBw> getByYear(String tynsrsbh, String year) {
        String ssqq = year + "-01-01";
        String ssqz = year + "-12-31";
        LambdaQueryWrapper<SbCwbbBw> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SbCwbbBw::getTynsrsbh, tynsrsbh).ge(SbCwbbBw::getSkssqq, ssqq).lt(SbCwbbBw::getSkssqq, ssqz);
        queryWrapper.orderByAsc(SbCwbbBw::getSkssqz);
        return this.list(queryWrapper);
    }

    @Override
    public String getMaxJbSsqByNsr(String tynsrsbh) {
        return baseMapper.getMaxJbSsqByNsr(tynsrsbh);
    }

    @Override
    public String getMaxNbSsqByNsr(String tynsrsbh) {
        String maxSsq = baseMapper.getMaxNbSsqByNsr(tynsrsbh);
        if (StringUtils.isNotBlank(maxSsq) && maxSsq.length() >= 4) {
            return maxSsq.substring(0, 4);
        }
        return maxSsq;
    }

    @Override
    public CwbbSssjVo getCwbbSssj(String tynsrsbh, String ssqq, String ssqz) {
        SbCwbbBw sbCwbbBw = this.getByCompositePk(tynsrsbh, ssqq, ssqz);
        //进行封装
        if (sbCwbbBw != null) {
            String cwbbJson = sbCwbbBw.getJsonBw();
            try {
                GsCwbbVo gsCwbbVo = JSONObject.parseObject(cwbbJson, new TypeReference<GsCwbbVo>() {});
                CwbbSssjVo cwbbSssjVo = wrapperCwbbSssj(tynsrsbh,ssqq, ssqz, gsCwbbVo);
                return cwbbSssjVo;
            } catch (Exception e) {
                e.printStackTrace();
                log.error("解析财报报表数据失败", e);
                throw new JeecgBootException("解析财报报表数据失败");
            }
        } else {
            return new CwbbSssjVo();
        }
    }

    /**
     * 将财报数据转成需要的指标数据
     * @param gsCwbbVo
     * @return
     */
    private CwbbSssjVo wrapperCwbbSssj(String tynsrsbh, String skssqq, String skssqz, GsCwbbVo gsCwbbVo) {
        CwbbSssjVo cwbbSssjVo = new CwbbSssjVo();
        List<CwbbSssjVo.LowDataVo> lrbList = new ArrayList<>();
        List<CwbbSssjVo.LowDataVo> xjllbList = new ArrayList<>();
        List<CwbbSssjVo.LowDataVo> zcfzbList = new ArrayList<>();
        //利润表，现金流量表，资产负债表中需要提取的指标
        String[][] lrbKeyNames = new String[][]{
                {"yylr","营业收入"},
                {"yycb","营业成本"},
                {"xsfy","销售费用"},
                {"glfy","管理费用"},
                {"cwfy","财务费用"},
                {"tzsy","投资收益"},
                {"yylr","营业利润"},
                {"yywsr","营业外收入"},
                {"yywzc","营业外支出"},
                {"lrze","利润总额"},
                {"jlr","净利润"}
        };
        String[][] zcfzbKeyNames = new String[][] {
                {"hbzj","货币资金"},
                {"yspj","应收票据"},
                {"yszkze","应收账款总额"},
                {"yfzkZc","预付账款"},
                {"qtyszk","其他应收账款"},
                {"chze","存货总额"},
                {"htzc","合同资产"},
                {"ldzchj","流动资产合计"},
                {"cqgqtz","长期股权投资"},
                {"tzxfdc","投资性房地产"},
                {"gdzc","固定资产"},
                {"wxzc","无形资产"},
                {"fldzchj","非流动资产合计"},
                {"zcze","资产总额"},
                {"dqjk","短期借款"},
                {"yfpj","应付票据"},
                {"yfzkQy","应付账款"},
                {"yszk","预收账款"},
                {"qtyfk","其他应付款"},
                {"ldfzhj","流动负债合计"},
                {"cqjk","长期借款"},
                {"yfzj","应付债券"},
                {"cqyfk","长期应付款"},
                {"yjfz","预计负债"},
                {"fldfzhj","非流动负债合计"},
                {"fzze","负债总额"},
                {"sszb","实收资本"},
                {"syzqy","所有者权益"}
        };
        String[][] xjllbKeyNames = new String[][] {
                {"jyhdcsdxjllje","经营活动产生的现金流量净额"},
                {"czhdcsdxjllje","筹资活动产生的现金流量净额"},
                {"tzhdcsdxjllje","投资活动产生的现金流量净额"},
                {"xjjzje","现金净增加额"},
                {"qcxjye","期初现金余额"},
                {"qmxjye","期末现金余额"}
        };
        //资产负债表的类型 1-资产，2-权限
        Integer[] zcfzbTypes  = new Integer[]{1,1,1,1,1,1,1,1, 1, 1, 1, 1, 1, 1, 2,2,2,2,2, 2, 2, 2, 2, 2, 2, 2, 2, 2};

        boolean isYear = false;
        if (skssqq.endsWith("01-01") && skssqz.endsWith("12-31")) {
            isYear = true;
        }

        //适用于101，102，201三个才保存原始报文
        //一般企业
        if (gsCwbbVo.getYbqylrbVO() != null
                || gsCwbbVo.getYbqyxjllbVO() != null
                || gsCwbbVo.getYbqyzcfzbVO() != null
                || gsCwbbVo.getYbqylrbyzxVO() != null
                || gsCwbbVo.getYbqyzcfzbyzxVO() != null) {
            //判断是否已执行新金融
            if (gsCwbbVo.getYbqylrbyzxVO() != null
                    || gsCwbbVo.getYbqyzcfzbyzxVO() != null) {
                cwbbSssjVo.setKjzdzzDm("101-1");
                //利润表实体封装
                if (gsCwbbVo.getYbqylrbyzxVO() != null
                        && gsCwbbVo.getYbqylrbyzxVO().getYbqylrbyzxGrid() != null
                        && gsCwbbVo.getYbqylrbyzxVO().getYbqylrbyzxGrid().getYbqylrbyzxGridlb() != null) {
                    List<YbqylrbyzxGridlb> lrbGridlb = gsCwbbVo.getYbqylrbyzxVO().getYbqylrbyzxGrid().getYbqylrbyzxGridlb();
                    Map<String, YbqylrbyzxGridlb> ewbhxhMap = lrbGridlb.stream().collect(Collectors.toMap(YbqylrbyzxGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    //表中的行序
                    Integer[] indexs = new Integer[]{1,2,4,5,7,11,19,20,21,22,24};
                    wrapperCwbbSssjYbqyyzxLrb(lrbList, ewbhxhMap, lrbKeyNames, indexs);
                }

                //现金流量表实体封装
                if (gsCwbbVo.getYbqyxjllbVO() != null
                        && gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid() != null
                        && gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid().getYbqyxjllbGridlb() != null) {
                    List<YbqyxjllbGridlb> xjllbGridlb = gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid().getYbqyxjllbGridlb();
                    Map<String, YbqyxjllbGridlb> ewbhxhMap = xjllbGridlb.stream().collect(Collectors.toMap(YbqyxjllbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{10, 31, 22, 33, 34, 35};
                    wrapperCwbbSssjYbqyyzxXjllb(xjllbList, ewbhxhMap, xjllbKeyNames, indexs);
                }

                //资产负债表实体封装
                if (gsCwbbVo.getYbqyzcfzbyzxVO() != null
                        && gsCwbbVo.getYbqyzcfzbyzxVO().getYbqyzcfzbzbyzxGrid() != null
                        && gsCwbbVo.getYbqyzcfzbyzxVO().getYbqyzcfzbzbyzxGrid().getYbqyzcfzbzbyzxGridlb() != null) {
                    List<YbqyzcfzbzbyzxGridlb> zbzbGridlb = gsCwbbVo.getYbqyzcfzbyzxVO().getYbqyzcfzbzbyzxGrid().getYbqyzcfzbzbyzxGridlb();
                    Map<String, YbqyzcfzbzbyzxGridlb> ewbhxhMap = zbzbGridlb.stream().collect(Collectors.toMap(YbqyzcfzbzbyzxGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{1,4,5,7,8,9,10,14,18,21,22,27,33,34,1,4,5,6,10,14,15,16,20,21,25,26,28,38};
                    wrapperCwbbSssjYbqyyzxZcfzb(zcfzbList, ewbhxhMap, zcfzbKeyNames, indexs, zcfzbTypes);
                }
            } else {
                cwbbSssjVo.setKjzdzzDm("101");
                //利润表实体封装
                if (gsCwbbVo.getYbqylrbVO() != null
                        && gsCwbbVo.getYbqylrbVO().getYbqylrbGrid() != null
                        && gsCwbbVo.getYbqylrbVO().getYbqylrbGrid().getYbqylrbGridlb() != null) {
                    List<YbqylrbGridlb> ybqylrbGridlbList = gsCwbbVo.getYbqylrbVO().getYbqylrbGrid().getYbqylrbGridlb();
                    Map<String, YbqylrbGridlb> ewbhxhMap = ybqylrbGridlbList.stream().collect(Collectors.toMap(YbqylrbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{1,2,4,5,6,9,11,12,13,15,17};
                    wrapperCwbbSssjYbqyLrb(lrbList, ewbhxhMap, lrbKeyNames, indexs);
                }
                //现金流量表实体封装
                if (gsCwbbVo.getYbqyxjllbVO() != null
                        && gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid() != null
                        && gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid().getYbqyxjllbGridlb() != null) {
                    List<YbqyxjllbGridlb> ybqyxjllbGridlbList = gsCwbbVo.getYbqyxjllbVO().getYbqyxjllbGrid().getYbqyxjllbGridlb();
                    Map<String, YbqyxjllbGridlb> ewbhxhMap = ybqyxjllbGridlbList.stream().collect(Collectors.toMap(YbqyxjllbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[] {10,31,22,33,34,35};
                    wrapperCwbbSssjYbqyXjllb(xjllbList, ewbhxhMap, xjllbKeyNames, indexs);
                }
                if (gsCwbbVo.getYbqyzcfzbVO() != null
                        && gsCwbbVo.getYbqyzcfzbVO().getYbqyzcfzbzbGrid() != null
                        && gsCwbbVo.getYbqyzcfzbVO().getYbqyzcfzbzbGrid().getYbqyzcfzbzbGridlb() != null) {
                    List<YbqyzcfzbzbGridlb> gridlbList = gsCwbbVo.getYbqyzcfzbVO().getYbqyzcfzbzbGrid().getYbqyzcfzbzbGridlb();
                    Map<String, YbqyzcfzbzbGridlb> ewbhxhMap = gridlbList.stream().collect(Collectors.toMap(YbqyzcfzbzbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{1,3,4,5,8,9,null,12,17,18,19,25,31,32,1,3,4,5,10,13,15,16,17,19,22,23,25,30};
                    wrapperCwbbSssjYbqyZcfzb(zcfzbList, ewbhxhMap, zcfzbKeyNames, indexs, zcfzbTypes);
                }
            }
        } else if (gsCwbbVo.getSyxqylrb() != null
                || gsCwbbVo.getSyxqyxjllb() != null
                || gsCwbbVo.getSyxqyzcfzb() != null) {
            cwbbSssjVo.setKjzdzzDm("102");
            //小企业年报
            if (isYear == true) {
                if (gsCwbbVo.getXqylrbnb() != null
                        && gsCwbbVo.getXqylrbnb().getSyxqylrbnbGrid() != null
                        && gsCwbbVo.getXqylrbnb().getSyxqylrbnbGrid().getXqylrbnbGridlb() != null) {
                    List<XqylrbnbGridlb> xqylrbnbGridlb = gsCwbbVo.getXqylrbnb().getSyxqylrbnbGrid().getXqylrbnbGridlb();
                    Map<String, XqylrbnbGridlb> ewbhxhMap = xqylrbnbGridlb.stream().collect(Collectors.toMap(XqylrbnbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{1, 2, 11, 14, 18, 20, 21, 22, 24, 30, 32};
                    wrapperCwbbSssjXqyLrb(lrbList, ewbhxhMap, lrbKeyNames, indexs);
                }
                if (gsCwbbVo.getSyxqyxjllbnb() != null
                        && gsCwbbVo.getSyxqyxjllbnb().getXqyxjllbnGrid() != null
                        && gsCwbbVo.getSyxqyxjllbnb().getXqyxjllbnGrid().getXqyxjllbnGridlb() != null) {
                    List<XqyxjllbnGridlb> xqyxjllbGridlb = gsCwbbVo.getSyxqyxjllbnb().getXqyxjllbnGrid().getXqyxjllbnGridlb();
                    Map<String, XqyxjllbnGridlb> ewbhxhMap = xqyxjllbGridlb.stream().collect(Collectors.toMap(XqyxjllbnGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[] {7,19,13,20,21,22};
                    wrapperCwbbSssjXqyXjllb(xjllbList, ewbhxhMap, xjllbKeyNames, indexs);
                }
            } else {
                if (gsCwbbVo.getSyxqylrb() != null
                        && gsCwbbVo.getSyxqylrb().getSyxqylrbGrid() != null
                        && gsCwbbVo.getSyxqylrb().getSyxqylrbGrid().getSyxqylrbGridlb() != null) {
                    List<XqylrbnbGridlb> syxqylrbGridlb = gsCwbbVo.getSyxqylrb().getSyxqylrbGrid().getSyxqylrbGridlb();
                    Map<String, XqylrbnbGridlb> ewbhxhMap = syxqylrbGridlb.stream().collect(Collectors.toMap(XqylrbnbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[]{1, 2, 11, 14, 18, 20, 21, 22, 24, 30, 32};
                    wrapperCwbbSssjXqyLrb(lrbList, ewbhxhMap, lrbKeyNames, indexs);
                }
                if (gsCwbbVo.getSyxqyxjllb() != null
                        && gsCwbbVo.getSyxqyxjllb().getXqyxjllbGrid() != null
                        && gsCwbbVo.getSyxqyxjllb().getXqyxjllbGrid().getXqyxjllbGridlb() != null) {
                    List<XqyxjllbnGridlb> xqyxjllbGridlb = gsCwbbVo.getSyxqyxjllb().getXqyxjllbGrid().getXqyxjllbGridlb();
                    Map<String, XqyxjllbnGridlb> ewbhxhMap = xqyxjllbGridlb.stream().collect(Collectors.toMap(XqyxjllbnGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                    Integer[] indexs = new Integer[] {7,19,13,20,21,22};
                    wrapperCwbbSssjXqyXjllb(xjllbList, ewbhxhMap, xjllbKeyNames, indexs);
                }
            }

            if (gsCwbbVo.getSyxqyzcfzb() != null
                    && gsCwbbVo.getSyxqyzcfzb().getSyxqyzcfzbGrid() != null
                    && gsCwbbVo.getSyxqyzcfzb().getSyxqyzcfzbGrid().getXqyzcfzbGridlb() != null) {
                List<XqyzcfzbGridlb> xqyzcfzbGridlb = gsCwbbVo.getSyxqyzcfzb().getSyxqyzcfzbGrid().getXqyzcfzbGridlb();
                Map<String, XqyzcfzbGridlb> ewbhxhMap = xqyzcfzbGridlb.stream().collect(Collectors.toMap(XqyzcfzbGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                Integer[] indexs = new Integer[]{1, 3, 4, 5, 8, 9, null, 15, 18, null, 21, 26, 30, 31, 1, 2, 3, 4, 9, 11, 13, null, 14, null, 17, 18, 26, 30};
                wrapperCwbbSssjXqyZcfzb(zcfzbList, ewbhxhMap, zcfzbKeyNames, indexs, zcfzbTypes);
            }
        } else if (gsCwbbVo.getQykjzdcwbbzcfzbVO() != null) {
            cwbbSssjVo.setKjzdzzDm("201");
            if (gsCwbbVo.getQykjzdcwbblrbVO() != null
                    && gsCwbbVo.getQykjzdcwbblrbVO().getQykjzdcwbblrbxxGrid() != null
                    && gsCwbbVo.getQykjzdcwbblrbVO().getQykjzdcwbblrbxxGrid().getQykjzdcwbblrbxxGridlb() != null) {
                List<QykjzdcwbblrbxxGridlb> qykjzdcwbblrbxxGridlb = gsCwbbVo.getQykjzdcwbblrbVO().getQykjzdcwbblrbxxGrid().getQykjzdcwbblrbxxGridlb();
                Map<String, QykjzdcwbblrbxxGridlb> ewbhxhMap = qykjzdcwbblrbxxGridlb.stream().collect(Collectors.toMap(QykjzdcwbblrbxxGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                Integer[] indexs = new Integer[]{1,2,6,7,8,10,9,12,13,14,16};
                wrapperCwbbSssjQykjzzLrb(lrbList, ewbhxhMap, lrbKeyNames, indexs);
            }

            if (gsCwbbVo.getQykjzdcwbbxjllbVO() != null
                    && gsCwbbVo.getQykjzdcwbbxjllbVO().getQykjzdcwbbxjllbxxGrid() != null
                    && gsCwbbVo.getQykjzdcwbbxjllbVO().getQykjzdcwbbxjllbxxGrid().getQykjzdcwbbxjllbxxGridlb() != null) {
                List<QykjzdcwbbxjllbxxGridlb> qykjzdcwbbxjllbxxGridlb = gsCwbbVo.getQykjzdcwbbxjllbVO().getQykjzdcwbbxjllbxxGrid().getQykjzdcwbbxjllbxxGridlb();
                Map<String, QykjzdcwbbxjllbxxGridlb> ewbhxhMap = qykjzdcwbbxjllbxxGridlb.stream().collect(Collectors.toMap(QykjzdcwbbxjllbxxGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                Integer[] indexs = new Integer[] {10,29,20,56,53,52};
                wrapperCwbbSssjQykjzzXjllb(xjllbList, ewbhxhMap, xjllbKeyNames, indexs);
            }

            if (gsCwbbVo.getQykjzdcwbbzcfzbVO() != null
                    && gsCwbbVo.getQykjzdcwbbzcfzbVO().getBdxxGrid() != null
                    && gsCwbbVo.getQykjzdcwbbzcfzbVO().getBdxxGrid().getZcfzbdxxGridlb() != null) {
                List<ZcfzbdxxGridlb> zcfzbdxxGridlb = gsCwbbVo.getQykjzdcwbbzcfzbVO().getBdxxGrid().getZcfzbdxxGridlb();
                Map<String, ZcfzbdxxGridlb> ewbhxhMap = zcfzbdxxGridlb.stream().collect(Collectors.toMap(ZcfzbdxxGridlb::getEwbhxh, Function.identity(), (e1, e2) -> e2));
                Integer[] indexs = new Integer[] {1,3,6,8,7,10,null,14,12,null,27,29,null,36,1,2,3,4,10,15,17,18,19,12,null,25,30,35};
                wrapperCwbbSssjQykjzzZcfzb(zcfzbList, ewbhxhMap, zcfzbKeyNames, indexs, zcfzbTypes);
            }
        }

        //计算每个表的行内值和偏差
        SssjCwbbHnzLrb hnzLrb = sssjCwbbHnzLrbService.selectByMultiId(new SssjCwbbHnzLrb().setDjxh(tynsrsbh).setSkssqq(skssqq).setSkssqz(skssqz));
        SssjCwbbHnzXjllb hnzXjllb = sssjCwbbHnzXjllbService.selectByMultiId(new SssjCwbbHnzXjllb().setDjxh(tynsrsbh).setSkssqq(skssqq).setSkssqz(skssqz));
        SssjCwbbHnzZcfzb hnzZcfzb = sssjCwbbHnzZcfzbService.selectByMultiId(new SssjCwbbHnzZcfzb().setDjxh(tynsrsbh).setSkssqq(skssqq).setSkssqz(skssqz));
        if (hnzLrb == null) {
            hnzLrb = new SssjCwbbHnzLrb();
        }
        if (hnzXjllb == null) {
            hnzXjllb= new SssjCwbbHnzXjllb();
        }
        if (hnzZcfzb == null) {
            hnzZcfzb = new SssjCwbbHnzZcfzb();
        }
        JSONObject hnzLrbJson = (JSONObject)JSONObject.toJSON(hnzLrb);
        lrbList.forEach( e -> {
            String key = e.getKey();
            BigDecimal hnz = hnzLrbJson.getBigDecimal(key);
            if (hnz != null) {
                e.setHnz(hnz);
                Double value1 = e.getValue1();
                if (value1 != null) {
                    if (hnz.compareTo(BigDecimal.ZERO) == 0) {
                        e.setBc(null);
                    } else {
                        BigDecimal valueDcm = new BigDecimal(value1);
                        BigDecimal bcz = valueDcm.subtract(hnz).divide(hnz, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        e.setBc(bcz.abs() + "%");
                    }
                }
            }
        });
        JSONObject hnzXjllbJson = (JSONObject)JSONObject.toJSON(hnzXjllb);
        xjllbList.forEach(e -> {
            String key = e.getKey();
            BigDecimal hnz = hnzXjllbJson.getBigDecimal(key);
            if (hnz != null) {
                e.setHnz(hnz);
                Double value1 = e.getValue1();
                if (value1 != null) {
                    if (hnz.compareTo(BigDecimal.ZERO) == 0) {
                        e.setBc(null);
                    } else {
                        BigDecimal valueDcm = new BigDecimal(value1);
                        BigDecimal bcz = valueDcm.subtract(hnz).divide(hnz, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        e.setBc(bcz.abs() + "%");                    }
                }
            }
        });
        JSONObject hnzZcfzbJson = (JSONObject)JSONObject.toJSON(hnzZcfzb);
        zcfzbList.forEach(e -> {
            String key = e.getKey();
            BigDecimal hnz = hnzZcfzbJson.getBigDecimal(key);
            if (hnz != null) {
                e.setHnz(hnz);
                Double value1 = e.getValue1();
                if (value1 != null) {
                    if (hnz.compareTo(BigDecimal.ZERO) == 0) {
                        e.setBc(null);
                    } else {
                        BigDecimal valueDcm = new BigDecimal(value1);
                        BigDecimal bcz = valueDcm.subtract(hnz).divide(hnz, 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        e.setBc(bcz.abs() + "%");                    }
                }
            }
        });
        cwbbSssjVo.setLrbList(lrbList);
        cwbbSssjVo.setXjllbList(xjllbList);
        cwbbSssjVo.setZcfzbList(zcfzbList);
        return cwbbSssjVo;
    }

    /**
     * 一般企业（新金融） 利润表
     * @param lrbList
     * @param ewbhxhMap
     */
    private void wrapperCwbbSssjYbqyyzxLrb(List<CwbbSssjVo.LowDataVo> lrbList, Map<String, YbqylrbyzxGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(CwbbYbqyyzxWrapperUtil.getLrbBqje(ewbhxhMap, indexs[i]))
                        .value2(CwbbYbqyyzxWrapperUtil.getLrbSqje(ewbhxhMap, indexs[i]))
                        .build();
            }
            lrbList.add(lowDataVo);
        }
    }

    /**
     * 一般企业 利润表
     */
    private void wrapperCwbbSssjYbqyLrb(List<CwbbSssjVo.LowDataVo> lrbList, Map<String, YbqylrbGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(CwbbYbqyWrapperUtil.getLrbBqje(ewbhxhMap, indexs[i]))
                        .value2(CwbbYbqyWrapperUtil.getLrbSqje(ewbhxhMap, indexs[i]))
                        .build();
            }
            lrbList.add(lowDataVo);
        }
    }

    /**
     * 小企业 利润表
     */
    private void wrapperCwbbSssjXqyLrb(List<CwbbSssjVo.LowDataVo> lrbList, Map<String, XqylrbnbGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
//                        .value1(CwbbXqyWrapperUtil.getLrbByje(ewbhxhMap, indexs[i]))
//                        .value2(CwbbXqyWrapperUtil.getLrbBnljje(ewbhxhMap, indexs[i]))
                        .value1(CwbbXqyWrapperUtil.getLrbBnljje(ewbhxhMap, indexs[i]))
                        .value2(CwbbXqyWrapperUtil.getLrbByje(ewbhxhMap, indexs[i]))
                        .build();
            }
            lrbList.add(lowDataVo);
        }
    }

    /**
     * 企业会计 利润表
     */
    private void wrapperCwbbSssjQykjzzLrb(List<CwbbSssjVo.LowDataVo> lrbList, Map<String, QykjzdcwbblrbxxGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
//                        .value1(CwbbQykjzzWrapperUtil.getLrbBys(ewbhxhMap, indexs[i]))
//                        .value2(CwbbQykjzzWrapperUtil.getLrbBnljs(ewbhxhMap, indexs[i]))
                        .value1(CwbbQykjzzWrapperUtil.getLrbBnljs(ewbhxhMap, indexs[i]))
                        .value2(CwbbQykjzzWrapperUtil.getLrbBys(ewbhxhMap, indexs[i]))
                        .build();
            }
            lrbList.add(lowDataVo);
        }
    }

    /**
     * 一般企业（新金融） 现金流量表
     */
    private void wrapperCwbbSssjYbqyyzxXjllb(List<CwbbSssjVo.LowDataVo> xjllbList, Map<String, YbqyxjllbGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(CwbbYbqyyzxWrapperUtil.getXjllbBqje(ewbhxhMap, indexs[i]))
                        .value2(CwbbYbqyyzxWrapperUtil.getXjllbSqje(ewbhxhMap, indexs[i]))
                        .build();
            }
            xjllbList.add(lowDataVo);
        }
    }

    /**
     * 一般企业 现金流量表
     */
    private void wrapperCwbbSssjYbqyXjllb(List<CwbbSssjVo.LowDataVo> xjllbList, Map<String, YbqyxjllbGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(CwbbYbqyWrapperUtil.getXjllbBqje(ewbhxhMap, indexs[i]))
                        .value2(CwbbYbqyWrapperUtil.getXjllbSqje(ewbhxhMap, indexs[i]))
                        .build();
            }
            xjllbList.add(lowDataVo);
        }
    }

    /**
     * 小企业 现金流量表
     */
    private void wrapperCwbbSssjXqyXjllb(List<CwbbSssjVo.LowDataVo> xjllbList, Map<String, XqyxjllbnGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        //.value1(CwbbXqyWrapperUtil.getXjllbByje(ewbhxhMap, indexs[i]))
                        //.value2(CwbbXqyWrapperUtil.getXjllbBnljje(ewbhxhMap, indexs[i]))
                        .value1(CwbbXqyWrapperUtil.getXjllbBnljje(ewbhxhMap, indexs[i]))
                        .value2(CwbbXqyWrapperUtil.getXjllbByje(ewbhxhMap, indexs[i]))
                        .build();
            }
            xjllbList.add(lowDataVo);
        }
    }


    /**
     * 企业会计制度 现金流量表
     */
    private void wrapperCwbbSssjQykjzzXjllb(List<CwbbSssjVo.LowDataVo> xjllbList, Map<String, QykjzdcwbbxjllbxxGridlb> ewbhxhMap, String[][] names, Integer[] indexs) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(CwbbQykjzzWrapperUtil.getXjllbJe(ewbhxhMap, indexs[i]))
                        .build();
            }
            xjllbList.add(lowDataVo);
        }
    }

    /**
     * 一般企业（新金融） 资产负债表
     * @param zcfzbList
     * @param ewbhxhMap
     * @param types  元素中值 1-资产（默认），2-权益
     */
    private void wrapperCwbbSssjYbqyyzxZcfzb(List<CwbbSssjVo.LowDataVo> zcfzbList, Map<String, YbqyzcfzbzbyzxGridlb> ewbhxhMap, String[][] names, Integer[] indexs, Integer[] types) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                if (types[i] != null && types[i] == 2) {
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbYbqyyzxWrapperUtil.getZcfzbQmyeQy(ewbhxhMap, indexs[i]))
                            .value2(CwbbYbqyyzxWrapperUtil.getZcfzbSnnmyeQy(ewbhxhMap, indexs[i]))
                            .build();
                } else {
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbYbqyyzxWrapperUtil.getZcfzbQmyeZc(ewbhxhMap, indexs[i]))
                            .value2(CwbbYbqyyzxWrapperUtil.getZcfzbSnnmyeZc(ewbhxhMap, indexs[i]))
                            .build();
                }
            }
            zcfzbList.add(lowDataVo);
        }
    }

    /**
     * 一般企业 资产负债表
     * @param zcfzbList
     * @param types  元素中值 1-资产（默认），2-权益
     */
    private void wrapperCwbbSssjYbqyZcfzb(List<CwbbSssjVo.LowDataVo> zcfzbList,  Map<String, YbqyzcfzbzbGridlb> ewbhxhMap, String[][] names, Integer[] indexs, Integer[] types) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                //权益
                if (types[i] != null && types[i] == 2) {
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbYbqyWrapperUtil.getZcfzbQmyeQy(ewbhxhMap, indexs[i]))
                            .value2(CwbbYbqyWrapperUtil.getZcfzbNcyeQy(ewbhxhMap, indexs[i]))
                            .build();
                } else {
                    //资产
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbYbqyWrapperUtil.getZcfzbQmyeZc(ewbhxhMap, indexs[i]))
                            .value2(CwbbYbqyWrapperUtil.getZcfzbNcyeZc(ewbhxhMap, indexs[i]))
                            .build();
                }

            }
            zcfzbList.add(lowDataVo);
        }
    }


    /**
     * 小企业 资产负债表
     */
    private void wrapperCwbbSssjXqyZcfzb(List<CwbbSssjVo.LowDataVo> zcfzbList, Map<String, XqyzcfzbGridlb> ewbhxhMap, String[][] names, Integer[] indexs, Integer[] types) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                //权限
                if (types[i] != null && types[i] == 2) {
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbXqyWrapperUtil.getZcfzQmyeQy(ewbhxhMap, indexs[i]))
                            .value2(CwbbXqyWrapperUtil.getZcfzNcyeQy(ewbhxhMap, indexs[i]))
                            .build();
                } else {
                    //资产
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbXqyWrapperUtil.getZcfzQmyeZc(ewbhxhMap, indexs[i]))
                            .value2(CwbbXqyWrapperUtil.getZcfzNcyeZc(ewbhxhMap, indexs[i]))
                            .build();
                }

            }
            zcfzbList.add(lowDataVo);
        }
    }

    /**
     * 企业会计制度 资产负债表
     */
    private void wrapperCwbbSssjQykjzzZcfzb(List<CwbbSssjVo.LowDataVo> zcfzbList,  Map<String, ZcfzbdxxGridlb> ewbhxhMap, String[][] names, Integer[] indexs, Integer[] types) {
        for (int i = 0; i < names.length; i++) {
            CwbbSssjVo.LowDataVo lowDataVo = null;
            if (indexs == null || indexs[i] == null || indexs[i] == 0) {
                lowDataVo = CwbbSssjVo.LowDataVo.builder()
                        .key(names[i][0])
                        .title(names[i][1])
                        .value1(null)
                        .value2(null)
                        .build();
            } else {
                //权限
                if (types[i] != null && types[i] == 2) {
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbQykjzzWrapperUtil.getZcfzQmsQy(ewbhxhMap, indexs[i]))
                            .value2(CwbbQykjzzWrapperUtil.getZcfzNcsQy(ewbhxhMap, indexs[i]))
                            .build();
                } else {
                    //资产
                    lowDataVo = CwbbSssjVo.LowDataVo.builder()
                            .key(names[i][0])
                            .title(names[i][1])
                            .value1(CwbbQykjzzWrapperUtil.getZcfzQmsZc(ewbhxhMap, indexs[i]))
                            .value2(CwbbQykjzzWrapperUtil.getZcfzNcsZc(ewbhxhMap, indexs[i]))
                            .build();
                }

            }
            zcfzbList.add(lowDataVo);
        }
    }

}
